﻿/*
 *  PassBag is multi-platform software that securely manage/store passwords
 *  Copyright (C) <2009>  <Hugo Rodrigues PEREIRA>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *	
 *  You can find a copy of the GPLv3 at project <root directory>/gpl.txt
 *
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PassBag.Logic.Backup;

namespace PassBag.Presentation.Backup {
    public partial class BackupSetAddDialog : PassBag.Presentation.Backup.BackupSetDialog {
        public BackupSetAddDialog () {
            InitializeComponent ();

            LoadRessource ();
        }

        #region Load Resource

        /// <summary>
        /// Loads the ressource.
        /// </summary>
        private void LoadRessource () {
            this.Text = Properties.Resources.BackupSetAddDialog_Text;
            dialogTitleLabel.Text = Properties.Resources.BackupSetAddDialog_Title;
            newEntryGroupBox.Text = Properties.Resources.BackupSetAddDialog_NewEntryGroupBox;
            activeFilelabel.Text = Properties.Resources.BackupSetAddDialog_ActiveFilelabel;
            activeFileCommentLabel.Text = Properties.Resources.BackupSetAddDialog_ActiveFileCommentLabel;

            addButton.Text = Properties.Resources.BackupSetAddDialog_AddFileButton;
            cancelButton.Text = Properties.Resources.Global_CancelButton;
        }

        #endregion

        private void addButton_Click (object sender, EventArgs e) {
            if (fileState == BackupFileState.FileExist) {
                BackupSet.AddFileToArchive (backupSetTextBox.Text, new BackupZipEntry (BackupSet.GetFileName (this.CurrentPasswordFile), activeFileCommentTextBox.Text));
                UpdateBackupSetItems ();
                this.DialogResult = DialogResult.OK;
            }
            else if (fileState == BackupFileState.OnlyDirectoryExist) {
                List<BackupZipEntry> backupList = new List<BackupZipEntry>();

                backupList.Add(new BackupZipEntry (BackupSet.GetFileName (this.CurrentPasswordFile), activeFileCommentTextBox.Text));

                BackupSet.CreateZipFiles (backupSetTextBox.Text, backupList);
                UpdateBackupSetItems ();
                this.DialogResult = DialogResult.OK;
            }
            else {
                MessageDialog.ShowError (this, Properties.Resources.BackupSetAddDialog_BackupSetLoadedError);
            }
        }

        public new DialogResult ShowDialog () {
            LoadBackupSetItems ();

            activeFileInfoLabel.Text = BackupSet.GetFileName (this.CurrentPasswordFile);
            activeFileCommentTextBox.Focus ();

            return base.ShowDialog ();
        }
    }
}
